﻿using System;
using System.Reflection;

namespace AFI.Speargun.Web.Core
{
    public class EnumTextValueAttribute : Attribute
    {
        public readonly string Text;
        public EnumTextValueAttribute(string text)
        {
            Text = text;
        }
    }

    public static class EnumExtension
    {
        public static string GetTextValue(this Enum e)
        {
            FieldInfo fi = e.GetType().GetField(e.ToString());

            EnumTextValueAttribute[] attributes = (EnumTextValueAttribute[])fi.GetCustomAttributes(typeof(EnumTextValueAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Text : e.ToString();
        }
    }
}
